
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class ViewImageForm extends Canvas implements CommandListener, Runnable {

    private byte[] data;
    private Image image;
    private HomeScreen homeScreen;
    private Command cmdSave;
    private Command cmdBack;

    public ViewImageForm(HomeScreen homeScreen) {
        this.homeScreen = homeScreen;
        cmdSave = new Command("Save", Command.OK, 2);
        cmdBack = new Command("Back", Command.BACK, 2);
        addCommand(cmdSave);
        addCommand(cmdBack);
        setCommandListener(this);
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        g.setColor(0);
        g.fillRect(0, 0, getWidth(), getHeight());
        if (image != null) {
            int x = 0;
            int y = 0;
            if (image.getWidth() < getWidth()) {
                x = (getWidth() - image.getWidth()) / 2;
            }
            if (image.getHeight() < getHeight()) {
                y = (getHeight() - image.getHeight()) / 2;
            }
            g.drawImage(image, x, y, 0);
        } else {
            g.setColor(0xffffff);
            g.setFont(Util.fontContent);
            g.drawString("Loading...", (getWidth() - Util.fontContent.stringWidth("Loading...")) / 2, (getHeight() - Util.fontContent.getHeight()) / 2, 0);
        }
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdSave) {
            try {
                String dirPhotos = System.getProperty("fileconn.dir.photos");
                String fileName = dirPhotos + "test.png";
                OutputStream outStream = null;
                FileConnection file = (FileConnection) Connector.open(fileName, Connector.READ_WRITE);
                if (file.exists() == false) {
                    file.create();
                }
                outStream = file.openOutputStream();
                outStream.write(data);
                if (outStream != null) {
                    outStream.close();
                }
                if (file != null) {
                    file.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else if (c == cmdBack) {
            homeScreen.DisplayListImageNameBack();
        }
    }

    public void run() {
        while (true) {
            if (Common.Client.lstImages != null && Common.Client.lstImages.size() > 0) {
                data = (byte[]) Common.Client.lstImages.elementAt(0);
                image = Image.createImage(data, 0, data.length);
                repaint();
                Common.Client.lstImages.removeElementAt(0);
                break;
            }
        }
    }

    protected void keyPressed(int keyCode) {
        int key = getGameAction(keyCode);
        switch (key) {
            case RIGHT:
                Common.Client.SendImageNext();
                image = null;
                repaint();
                new Thread(this).start();
                break;
            case LEFT:
                Common.Client.SendImagePre();
                image = null;
                repaint();
                new Thread(this).start();
                break;
        }
    }
}
